package persistent.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;

import basic.HibBaseDAO;
import basic.HibernateSessionFactory;
import persistent.factory.DAOFactory;
import model.TCheckReport;
import model.TCheckReportItem;
import model.TCloudClass;
import model.TStudent;
import model.VCheckReport;
import model.VClassCheckReport;
import model.VGradeCheckRport;
import model.VReportImport;
import model.VSchoolCheckReport;
import persistent.dao.ICheckReportDAO;
import util.toolkit.LogUtil;

public class CheckReportDAOImpl implements ICheckReportDAO {
	//定义日志管理对象
	private static final Log log = LogFactory.getLog(CheckReportDAOImpl.class);
	
	HibBaseDAO dao = new HibBaseDAO();
	@Override
	public List<VCheckReport> getReportByCondition(String wherecondition) {
		String hql = "from VCheckReport " + wherecondition;
		//System.out.println(hql);
		return dao.select(hql);
	}
	
	@Override
	public List<VCheckReport> getReportByCondition(String wherecondition,
			int currentPage, int pageSize) {
		String hql = "from VCheckReport " + wherecondition;
		return dao.selectByPage(hql, currentPage, pageSize);
	}
	
	/**
	 * 2018-12-10 20:21改
	 */
	@Override
	public boolean saveReportFromImport(List<VReportImport> reportlist) {
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = session.beginTransaction();
		//System.out.println("list.size() = " +reportlist.size());
		try{
			for(VReportImport report : reportlist){
				//将report中的报告信息添加到一个报告中
				TCheckReport treport = new TCheckReport();
				treport.setChecktypeid(report.getChecktypeid());
				treport.setUsername(report.getUsername());
				treport.setChecktime(report.getChecktime());
				treport.setItemscorestr(report.getItemscorestr());
				//System.out.println(report.getItemscorestr());
				//先保存一下学生信息
				report.getUsername();
				report.getRealName();
				report.getSex();
				report.getBirthday();
				report.getClassId();
				//先检测下报告所属学生是否存在,如果不存在则要在T_Student表中添加学生的信息
				if(!DAOFactory.getStudentDAO().isexist(report.getUsername())){
					//System.out.println("添加报告：" + report.getClassId()+"," + report.getUsername());
					//添加一个学生信息
					TStudent student = new TStudent();
					student.setStuid(report.getUsername());
					student.setRealName(report.getRealName());
					student.setSex(report.getSex());
					student.setBirthday(report.getBirthday());
					student.setClassid(report.getClassId());
					session.save(student); 
				}
				
				//保存一个测试报告对象
				Serializable key = session.save(treport);
				//System.out.println((Integer)key);

				//再遍历report中的itemlist
				List<TCheckReportItem> itemList = report.getCheckitems();
				for(TCheckReportItem item : itemList){
					item.setCheckid((Integer)key);
					//保存一个测试报告的结果栏目
					session.save(item);
					//System.out.println(item.getItemname());
				}	
			}
			tx.commit();
			return true;
		}catch(Exception e){
			if(tx!=null) tx.rollback();
			//session.close(); //关闭连接
			log.error("basic.HibBaseDAO类insert(Object obj)方法调用错误：");
			log.error(LogUtil.getMsg(e));
			System.out.println("basic.HibBaseDAO类insert(Object obj)方法调用错误：");
			e.printStackTrace();
		}
		return false;
	}


	@Override
	public boolean isExistCheckReport(int checktypeid,String username, String checktime) {
		String hql = "from VCheckReport where checktypeid = ? and "
				+ "username = ? and checktime = ?";
		Object[] para = {checktypeid,username,checktime};
		List list = dao.select(hql, para);
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
	}


	@Override
	public VCheckReport getVCheckReportByID(int id) {
		return (VCheckReport)dao.findById(VCheckReport.class, id);
	}


	@Override
	public List<VCheckReport> getReportsByUsername(String username) {
		String hql = "from VCheckReport where username = ?";
		Object[] para = {username};
		return dao.select(hql, para);
	}


	@Override
	public boolean delBatchCheckReport(String[] checkids) {
		Object obj[] = new Object[checkids.length]; 
		int model[] = new int[checkids.length];
		for(int i=0;i<checkids.length;i++){
			obj[i] = dao.findById(TCheckReport.class, Integer.parseInt(checkids[i]));
			model[i] = dao.DELETE;
		}
		return dao.executeBatch(obj,model);	
	}


	@Override
	public List<VClassCheckReport> getClassCheckReportsByCondition(
			String wherecondition) {
		String hql = "from VClassCheckReport " + wherecondition;
		return dao.select(hql);
	}
	
	@Override
	public List<VClassCheckReport> getClassCheckReportsByCondition(
			String wherecondition, int currentPage, int pageSize) {
		String hql = "from VClassCheckReport " + wherecondition;
		return dao.selectByPage(hql, currentPage, pageSize);
	}
	
	@Override
	public List<VGradeCheckRport> getGradeCheckRportByStrwhere(String strwhere) {
		String hql="from VGradeCheckRport "+strwhere;
		return dao.select(hql);
	}

	@Override
	public List<VGradeCheckRport> getGradeCheckRportByStrwhere(String strwhere,
			int currentPage, int pageSize) {
		String hql="from VGradeCheckRport "+strwhere;
		return dao.selectByPage(hql, currentPage, pageSize);
	}
	
	@Override
	public List<VSchoolCheckReport> getSchoolCheckReportByStrwhere(
			String strwhere) {
		String hql="from VSchoolCheckReport "+strwhere;
		return dao.select(hql);
	}

	@Override
	public List<VSchoolCheckReport> getSchoolCheckReportByStrwhere(
			String strwhere, int currentPage, int pageSize) {
		String hql="from VSchoolCheckReport "+strwhere;
		return dao.selectByPage(hql, currentPage, pageSize);
	}


	@Override
	public List<VCheckReport> getClassCheckReports(int checktypeid,
			String checktime, String classid) {
		String hql = "from VCheckReport where checktypeid = ? "
				+ "and checktime = ? and classid = ? order by realName";
		Object[] para = {checktypeid,checktime,classid};
		return dao.select(hql, para);
	}
	
	public static void main(String[] args){
		CheckReportDAOImpl dao = new CheckReportDAOImpl();
		List list = dao.getReportByCondition("");
		System.out.println(list.size());
	}


}
